<?php

namespace app\teacher\controller\core;

use app\common\controller\Frontend;
use app\common\enums\core\AuditStateEnum;
use app\common\model\banxi\core\CoreTrainerTimeApply as CoreTrainerTimeApplyModel;

/*
 * 教练请假
 * */

class CoreTrainerTimeApply extends Frontend
{
    protected array $noNeedLogin = [''];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }


    /*
     * 提交申请
     * */
    public function apply()
    {
        $applyDate = $this->request->post('applyDate', '');
        $applyEndTime = $this->request->post('applyEndTime', '');
        $applyStartTime = $this->request->post('applyStartTime', '');
        $applyType = $this->request->post('applyType', '');
        if (!$applyDate || !$applyEndTime || !$applyStartTime || !$applyType) {
            $this->fail('参数有误');
        }

        $user = $this->auth->getUserInfo();
        $CoreTrainerTimeApplyModel = new CoreTrainerTimeApplyModel();
        $insertData = [
            'trainerId' => $user['id'],
            'applyDate' => $applyDate,
            'applyStartTime' => $applyStartTime,
            'applyEndTime' => $applyEndTime,
            'applyType' => $applyType,
            'auditState' => AuditStateEnum::E1,
            'createTime' => time(),
            'updateTime' => time(),
        ];
        $result = $CoreTrainerTimeApplyModel->insert($insertData);
        $this->ok('操作成功', [], self::NOTIFICATION);
    }

    /*
     * 查询-我的申请记录
     * */
    public function listMyTimeApply()
    {
        $user = $this->auth->getUserInfo();
        $CoreTrainerTimeApplyModel = new CoreTrainerTimeApplyModel();
        $result = $CoreTrainerTimeApplyModel->where('trainerId', $user['id'])->order('id', 'desc')->select();

        $this->ok('操作成功', $result);
    }

    /*
     * 删除
     * */
    public function del()
    {

        $id = $this->request->post('id', 0);

        $user = $this->auth->getUserInfo();

        $CoreTrainerTimeApplyModel = new CoreTrainerTimeApplyModel();
        $log = $CoreTrainerTimeApplyModel->destroy(['id' => $id, 'trainerId' => $user['id']]);

        $this->ok('操作成功');
    }
}